import { readFileSync } from 'fs';
import * as jsYaml from 'js-yaml';
import { join } from 'path';
// import { AllAllowObject } from 'src/types';
// import { deepMerge } from 'src/utils';

const CONFIG_PATH_STATIC = `env.yml`;
const targetPathStatic: string = join(
  __dirname,
  '../../config',
  CONFIG_PATH_STATIC,
);

// const envVar: string = process.env.NODE_ENV || 'development';
// const CONFIG_PATH_DYNAMIC = `env.${envVar}.ym/Users/ninebot/D/node/nest/demo2/configl`;
// const targetPathDynamic: string = join(
//   __dirname,
//   '../../config',
//   CONFIG_PATH_DYNAMIC,
// );

// 读取 YAML 文件并解析为对象
const loadYaml = (filePath: string): unknown => {
  try {
    const fileContents: any = readFileSync(filePath, 'utf8');
    // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
    return jsYaml.load(fileContents) as Record<string, any>;
  } catch (e) {
    console.error(`Error loading YAML file ${filePath}:`, e);
    return {};
  }
};

export default (): any => {
  try {
    const fileContentStatic = loadYaml(targetPathStatic);
    // const fileContentDynamic: AllAllowObject = loadYaml(targetPathDynamic);
    // const fileContent = deepMerge(fileContentStatic, fileContentDynamic);
    console.log('fileContentStatic', fileContentStatic);

    return fileContentStatic;
  } catch (error) {
    console.error('Failed to load configuration:', error);
    throw new Error('Configuration file could not be loaded.');
  }
};
